package com.qifan.energy.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.qifan.energy.bean.result.CartListResult;
import com.qifan.energy.mvp.callback.ICartCountCallback;
import com.qifan.energy.mvp.callback.ICheckCallback;
import com.qifan.energy.R;
import com.qifan.energy.global.GlideApp;
import com.qifan.energy.http.net.NetUtils;
import com.qifan.energy.http.response.MyObserver;
import com.qifan.energy.util.ToastUtils;
import com.qifan.energy.util.UIUtils;
import com.orhanobut.logger.Logger;

import java.util.List;

public class CartListAdapter extends BaseQuickAdapter<CartListResult,BaseViewHolder> {

    private ICheckCallback callback = new ICheckCallback() {
        @Override
        public void onSelect(boolean isSelected, CartListResult item) {

        }
    };

    private ICartCountCallback countCallback = new ICartCountCallback() {

        @Override
        public void onClick(boolean isAdd) {

        }
    };

    /* 对外提供 */
    public void setCheckCallback(ICheckCallback callback) {
        this.callback = callback;
    }

    public void setCountCallback(ICartCountCallback callback) {
        this.countCallback = callback;
    }

    public CartListAdapter(@Nullable List<CartListResult> data) {
        super(R.layout.card_list_item, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, CartListResult item) {
        ImageView productIv = helper.getView(R.id.product_iv);
        if (!TextUtils.isEmpty(item.getProductPic())) {
            GlideApp.with(helper.itemView)
                    .load(item.getProductPic())
                    .placeholder(R.mipmap.icon_default_rect)
                    .error(R.mipmap.icon_default_rect)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(productIv);
        }
        helper.setText(R.id.price_tv,String.format("￥%s",item.getPrice()))
                .setText(R.id.name_tv,item.getProductName());

        /* 添加事件 */
        ImageView circleIv = helper.getView(R.id.circle_iv);
        circleIv.setSelected(item.isSelected());
        circleIv.setOnClickListener(v -> {
            v.setSelected(!v.isSelected());
            item.setSelected(v.isSelected());
            if (callback != null) callback.onSelect(v.isSelected(),item);
        });
    }
}
